﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace AntikCompta.Data
{
    public class Caisse
    {
        private List<CaisseElement> mRecettes;
        private List<CaisseElement> mDepences;

        public Caisse()
        {
            mRecettes = new List<CaisseElement>();
            mDepences = new List<CaisseElement>();
        }

        public bool GetDepense(int aRef, out CaisseElement aDepense)
        {
            aDepense = null;
            IEnumerator<CaisseElement> iter = mDepences.GetEnumerator();
            while ((iter.MoveNext()) && (aDepense== null))
            {
                if (iter.Current.Reference == aRef)
                    aDepense = iter.Current;
            }

            return aDepense!=null;
        }

        public bool GetRecette(int aRef, out CaisseElement aRecette)
        {
            aRecette = null;
            IEnumerator<CaisseElement> iter = mRecettes.GetEnumerator();
            while ((iter.MoveNext()) && (aRecette == null))
            {
                if (iter.Current.Reference == aRef)
                    aRecette = iter.Current;
            }
            return aRecette != null;
        }

        [XmlArray("Recettes"), XmlArrayItem("recette")]
        public List<CaisseElement> Recettes
        {
            get { return mRecettes; }
            set { mRecettes=value; }
        }

        [XmlArray("Depences"), XmlArrayItem("depence")]
        public List<CaisseElement> Depences
        {
            get { return mDepences; }
            set { mDepences=value; }
        }

        internal void SupprimerElement(int aReference)
        {
            CaisseElement elmt;
            if (GetDepense(aReference, out elmt) == true)
                mDepences.Remove(elmt);
            else if (GetRecette(aReference, out elmt) == true)
                mRecettes.Remove(elmt);
        }
    }

    public class CaisseElement
    {
        static private int COUNTER = 0;
        private int mRef;
        private DateTime mDate;
        private String mDescription;
        private float mSomme;
        private String mCommentaire;

        public CaisseElement()
        {
            mRef = COUNTER;
            COUNTER++;
        }

        public int Reference
        {
            get { return mRef; }
        }

        public CaisseElement(DateTime aDate, String aDescription, float aSomme, String aCommentaire)
        {
            mDate = aDate;
            mDescription = aDescription;
            mSomme= aSomme;
            mCommentaire = aCommentaire;

            mRef = COUNTER;
            COUNTER++;
        }

        public CaisseElement(DateTime aDate, String aDescription, float aSomme)
        {
            mDate = aDate;
            mDescription = aDescription;
            mSomme = aSomme;

            mRef = COUNTER;
            COUNTER++;
        }

        public DateTime Date
        {
            get { return mDate; }
            set { mDate = value; }
        }

        public String Description
        {
            get { return mDescription; }
            set { mDescription = value; }
        }

        public float Somme
        {
            get { return mSomme; }
            set { mSomme = value; }
        }

        public String Commentaires
        {
            get { return mCommentaire; }
            set { mCommentaire = value; }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public static bool operator==(CaisseElement c1, CaisseElement c2)
        {
            // If both are null, or both are same instance, return true.
            if (System.Object.ReferenceEquals(c1, c2))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if (((object)c1 == null) || ((object)c2 == null))
            {
                return false;
            }

            // Return true if the fields match:
            return ((c1.Date == c2.Date) && (c1.Description == c2.Description) && (c1.Somme == c2.Somme));
        }

        public static bool operator !=(CaisseElement c1, CaisseElement c2)
        {
            return !(c1==c2);
        }
    }
}
